Ext.define('vacake.controller.Login', {
    extend: 'Ext.app.Controller',

    config: {
        routes: {
            'loadLogin': 'loadLogin'
        },
        refs: {
            mainView: 'mainView',
            loginView: 'loginForm',
            backParentButton: 'button[action=login_back_parent]',
            //  showPasswordButton: 'button[action=showPassword]',
            showPasswordButton: 'loginForm #showPassword',
            forgetPasswordButton: 'button[action=forgetPassword]',
            loginSubmitButton: 'button[action=loginSubmit]',
            registerLoadButton: 'button[action=registerLoad]',
            loginFieldView: '#login_field',
            textName: '#login_name',
            textPasswords: '#login_password',
            selectAreaButton: 'button[action=select_area]',
            loginButton: 'button[action=loginButton]',
            textLoginCaid: '#login_caid',
            textLoginAreaId: '#login_areaId'
        },
        control: {
            registerLoadButton: {
                tap: 'registerLoadButton_onTap'
            },
            backParentButton: {
                tap: 'backParentButton_onTap'
            },
            loginSubmitButton: {
                tap: 'loginSubmitButton_onTap'
            },
            showPasswordButton: {
                check: 'showPasswordView',
                uncheck: 'unShowPasswordView'
            },
            forgetPasswordButton:{
                tap:function(){
                    //if (window.localStorage.getItem('app_Login_id') == null || window.localStorage.getItem('app_Login_name') == null ) {
                    Ext.Viewport.animateActiveItem('findPassword', {type: 'slide', direction: 'left'});
                    //    return false;
                    //}
                }
            }
        }
    },
    /*点击注册按钮*/
    registerLoadButton_onTap: function () {
        Ext.Viewport.animateActiveItem('registerForm', {type: 'slide', direction: 'left'});
    },
    /*路由初始化*/
    loadLogin: function () {
        var loginView = this.getLoginView();
        if (loginView == null) {
            loginView = Ext.create("vacake.view.Login");
        }
        Ext.Viewport.animateActiveItem(loginView, {type: 'slide', direction: 'left'});
    },

    /*返回父窗口*/
    backParentButton_onTap: function () {
          var backid = Ext.Viewport.getInnerItems().length - 2;
         Ext.Viewport.remove(this.getLoginView(), true);
         Ext.Viewport.setActiveItem(backid);
    },
    /*点击登录按钮，提交成功后返回到首页中*/
    loginSubmitButton_onTap: function () {
        Ext.Viewport.mask({
            xtype: 'loadmask',
            message: '登录中...'
        });
        if (this.getLoginFieldView().child('textfield[name=viewLoginPassword]') != undefined) {
            this.getTextPasswords().setValue(this.getLoginFieldView().child('textfield[name=viewLoginPassword]').getValue());
        }
        var textName = this.getTextName().getValue();
        var passwords = this.getTextPasswords().getValue();
        if (textName == '' || passwords == '') {
            Ext.Viewport.unmask();
//            Ext.Msg.alert('手机，密码为空！');
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['手机，密码为空！']);
            return false;
        }
        this.getTextLoginCaid().setValue(Global.app_id);
        this.getTextLoginAreaId().setValue(window.localStorage.getItem('app_district_id'));
        this.getLoginView().submit({
            url: Global.api_url + '/cloud/1/users_login',
            method: 'POST',
            scope: this,
            success: function (from, result) {
                Ext.Viewport.unmask();
                cordova.exec(function (message) {
                }, function (error) {
                }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
                // Ext.Msg.alert(result.Variables.Message.messagestr);
                if (result.Variables.Result.code == 0) {
                    window.localStorage.setItem('app_Login_id', result.Variables.member_uid);//设置
                    window.localStorage.setItem('app_Login_name', result.Variables.member_username);
                    window.localStorage.setItem('app_Login_avatar', result.Variables.avatar);
                    //将登陆用户提交给cordova中
                    cordova.exec(function(winParam) {},function(error) {}, "GetApplicationInfo", "getApplicationInfo", [result.Variables.member_uid]);
                    Ext.Viewport.remove(this.getLoginView(), true);
                }
            },
            failure: function () {
                Ext.Viewport.unmask();
                //请求失败时执行操作
//                Ext.Msg.alert('请求失败', '服务器维护中....');
                cordova.exec(function (message) {
                }, function (error) {
                }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
            }
        });
    },
    /*显示和隐藏密码*/
    showPasswordView: function (checkBox, e) {
        var viewPassword = {xtype: 'textfield', name: 'viewLoginPassword', value: this.getTextPasswords().getValue(),  placeHolder: '密  码'};
        checkBox.setLabel('隐藏密码');
        this.getTextPasswords().hide();
        this.getLoginFieldView().add(viewPassword);
    },
    unShowPasswordView: function (checkBox, e) {
        checkBox.setLabel('显示密码');
        this.getTextPasswords().setValue(this.getLoginFieldView().child('textfield[name=viewLoginPassword]').getValue());
        this.getTextPasswords().show();
        this.getLoginFieldView().remove(this.getLoginFieldView().child('textfield[name=viewLoginPassword]'));
    }
});